package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    @PreAuthorize("hasAuthority('ROLE_ADD')")//新增角色权限
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {

        try {
            roleService.add(role, permissionIds,menuIds);
            return new Result(true, "角色添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色添加失败");
        }
    }
    @PreAuthorize("hasAuthority('ROLE_QUERY')")//查询角色权限
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findPage(queryPageBean);
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        List<Role> all = roleService.findAll();
        if (all != null && all.size() > 0) {
            return new Result(true, "角色查询成功", all);
        } else {
            return new Result(false, "角色查询失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer roleId) {
        try {
            Role role = roleService.findById(roleId);
            return new Result(true, "角色查询成功", role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"角色查询失败");
        }
    }

    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer roleId) {
        try {
            List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(roleId);
            return new Result(true, "角色查询成功", permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色查询失败");
        }
    }

    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer roleId) {
        try {
            List<Integer> menuIds = roleService.findMenuIdsByRoleId(roleId);
            return new Result(true, "角色查询成功", menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色查询失败");
        }
    }

    @PreAuthorize("hasAuthority('ROLE_EDIT')")//编辑角色权限
    @RequestMapping("/edit")
    public Result edit(@RequestBody  Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            roleService.edit(role, permissionIds,menuIds);
            return new Result(true, "角色编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色编辑失败");
        }
    }
    @PreAuthorize("hasAuthority('ROLE_DELETE')")//删除角色权限
    @RequestMapping("/delete.do")
    public Result delete(Integer id) {
        try {
            roleService.delete(id);
        } catch (RuntimeException e){
            return new Result(false, e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色删除失败");
        }
        return new Result(true, "角色删除成功");
    }
}
